// pages/my/electronSign/signManage/sign.js
var util = require('../../../../utils/util.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    signModeArray: [{
      'key': '手绘签署',
      'value': '1'
    }, {
      'key': '印章签署',
      'value': '2'
    }],
    mode_name: '请选择', // 签署方式
    seal_name: '请选择', // 印章名称
    disabledBtn: false, // 提交按钮状态
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.getStorage({
      key: 'userInfo',
      success: function(res) {
        that.setData({
          userInfo: res.data,
          sign_id: options.id
        });
        that.dataLoad();
      },
      fail: function() {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
    });
  },

  /**
   * 数据初始化
   */
  dataLoad: function() {
    wx.showLoading({
      title: '加载中···',
    });
    var that = this;
    util.getReq('/app/sign/getSignInfo', {
      sign_id: that.data.sign_id
    }, function(data) {
      if (data.code == 0) {
        var sign = data.data;
        that.setData({
          sign: sign,
          sealList: data.params.sealList,
          sign_type: sign.sign_type,
          seal_id: sign.seal_id,
          sealUrl: sign.preview_url,
          sign_keyword: sign.sign_keyword,
          seal_name: sign.seal_name
        });
        
        if (sign.sign_type == 1) {
          that.setData({
            mode_name: '手绘签署'
          });
        } else if (sign.sign_type == 2) {
          that.setData({
            mode_name: '印章签署'
          });
        }

        // 企业认证只允许印章签署
        if (sign.sign_auth_type == 1) {
          that.data.signModeArray.splice(0, 1);
          that.setData({
            signModeArray: that.data.signModeArray
          });
        }
      } else {
        util.isError(data.desc);
      }
      wx.hideLoading();
    });
  },

  /**
   * 签署关键字
   */
  sign_keyword: function(e) {
    this.setData({
      sign_keyword: e.detail.value
    });
  },

  /**
   * 签署方式选择
   */
  bindSignModeChange: function(e) {
    var that = this;
    var data = that.data.signModeArray[e.detail.value];
    that.setData({
      sign_type: data.value,
      mode_name: data.key
    });
  },

  /**
   * 印章选择
   */
  bindSealChange: function(e) {
    var that = this;
    var data = that.data.sealList[e.detail.value];
    that.setData({
      seal_id: data.seal_id,
      seal_name: data.seal_name,
      sealUrl: data.preview_url
    });
  },

  /**
   * 合同预览
   */
  openFile: function(e) {
    var that = this;
    var src = e.target.dataset.src;
    if (!src) {
      util.isError('文件不存在');
      return;
    }
    wx.downloadFile({
      url: src,
      success: function(res) {
        wx.openDocument({
          filePath: res.tempFilePath,
          fileType: 'pdf',
          success: function(res) {
            console.log('打开文件成功！');
          },
          fail: function(res) {
            console.log(res);
          },
          complete: function(res) {
            console.log(res);
          }
        });
      },
      fail: function() {
        console.log('下载失败！');
      }
    })
    console.log(e.target.dataset.src);
  },

  /**
   * 提交
   */
  submit: function() {
    var that = this;
    if (!that.submitCheck()) {
      return;
    }
    that.setData({
      disabledBtn: true
    });
    wx.showLoading({
      title: '提交中···',
    })
    var d = that.data;
    var data = {
      'sign_id': d.sign_id,
      'sign_keyword': d.sign_keyword,
      'sign_type': d.sign_type,
      'seal_id': d.seal_id
    };
    util.getReq('/app/sign/signFile', data, function(data) {
      if (data.code == 0) {
        wx.showToast({
          title: '提交成功',
        });
        wx.showModal({
          title: '提示',
          content: '提交成功，我们已向您手机发送签署短信，请注意查收',
          confirmText: '我知道了',
          showCancel: false,
          confirmColor: '#007aff',
        })
      } else {
        util.isError(data.desc);
        wx.hideLoading();
      }
      that.setData({
        disabledBtn: false
      });
    });
  },

  /**
   * 提交数据校验
   */
  submitCheck: function() {
    var that = this;
    var data = that.data;
    if (data.sign_keyword == '') {
      util.isError('请输入签署关键字');
      return false;
    }
    if (data.sign_type == 0) {
      util.isError('请选择签署方式');
      return false;
    }
    if (data.sign_type == 2 && data.seal_id == 0) {
      util.isError('请选择签署印章');
      return false;
    }
    return true;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.dataLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})